package cn.com.hatech.blog.module.admin.article.controller;


import cn.com.hatech.blog.common.controller.BaseController;
import cn.com.hatech.blog.common.result.RestResponse;
import cn.com.hatech.blog.module.admin.article.entity.Article;
import cn.com.hatech.blog.module.admin.article.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.blog.module.admin.controller
 * @className: ArticleController
 * @author: xuruikai
 * @description: 文章管理控制器
 * @date: 2020/1/11 14:27
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/v1/admin/article")
@Slf4j
public class ArticleRestController extends BaseController {
    /**
     * 文章内容业务层接口注入
     */
    @Resource
    private IArticleService contentsService;

    /**
     * 文章发表
     *
     * @param content
     * @return
     */
    @PostMapping(value = "/publish")
    @ResponseBody
    public RestResponse publishArticle(Article content) {
        int num = contentsService.publish(content);
        if (num > 0) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 文章更新
     *
     * @param content
     * @return
     */
    @PostMapping(value = "/modify")
    @ResponseBody
    public RestResponse modifyArticle(Article content) {
        int num = contentsService.updateArticle(content);
        if (num > 0) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public RestResponse delete(@RequestParam String id) {
        int num = contentsService.deleteById(id);
        if (num > 0) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
